package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.domain.AnswerContent;
import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IWendaStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.WendaStats;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/wenda")
public class WendaController {

    @Reference
    private IQuestionService questionService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IWendaStatsCacheService wendaStatsCacheService;

    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("")
    public String index(Model model){
        List<WendaStats> list = new ArrayList<>();
        model.addAttribute(
                "questions", questionService.selectAll());
        /*//redis查询本周回答数排行
        List<String> userIds = wendaStatsCacheService.rankList(RedisKey.WENDA_STATS_ANSWER, Answer.DATE_TYPE_WEEK);
        for (String userId : userIds) {
            WendaStats wendaStats = answerService.selectById(Long.valueOf(userId));
            wendaStats.setAnswernum(wendaStatsCacheService.getScore(RedisKey.WENDA_STATS_ANSWER, userId));
            list.add(wendaStats);
        }
        model.addAttribute(
                "rankList", list);*/
        return "wenda/wenda";
    }

    @RequestMapping("/question")
    public String question(Model model,@LoginUser UserInfo user){
        model.addAttribute(
                "user", user);
        return "wenda/public";
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model, @LoginUser UserInfo user){
        //浏览数加一
        model.addAttribute(
                "question", questionService.selectById(id));
        model.addAttribute(
                "user", user);
        return "wenda/wendaDetail";
    }

    @RequestMapping("/answer")
    @RequiredLogin
    @ResponseBody
    public AjaxResult answer(Answer answer, String content){
        //回答数加一
        wendaStatsCacheService.setAnswersDatasAsZset(RedisKey.WENDA_STATS_ANSWER,1 ,answer.getUserId());
        AjaxResult result = new AjaxResult();
        try{
            AnswerContent answerContent = new AnswerContent();
            answerContent.setContent(content);
            answer.setAnswerContent(answerContent);
            answerService.insert(answer);
        }catch(Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/questionAdd")
    @RequiredLogin
    @ResponseBody
    public AjaxResult questionAdd(Question question){
        AjaxResult result = new AjaxResult();
        try{
            questionService.insert(question);
        }catch(Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/rankList")
    public String rankList(int dataRank, int dataType, Model model){
        RedisKey key = RedisKey.WENDA_STATS_ANSWER;
        String type = "";
        switch(dataRank){
            case 0:key = RedisKey.WENDA_STATS_GOLD;break;//金牌
            case 1:key = RedisKey.WENDA_STATS_ANSWER;break;//回答
            case 2:key = RedisKey.WENDA_STATS_THUMBUP;break;//顶
        }
        switch(dataType){
            case 0:type = Answer.DATE_TYPE_DAY;break;//日
            case 1:type = Answer.DATE_TYPE_WEEK;break;//周
            case 2:type = Answer.DATE_TYPE_MONTH;break;//月
        }
        List<WendaStats> list = new ArrayList<>();
        //根据dataRank,dataType查询排行
        List<String> userIds = wendaStatsCacheService.rankList(key, type);
        for (String userId : userIds) {
            UserInfo userInfo = userInfoService.getById(Long.valueOf(userId));
            WendaStats wendaStat = new WendaStats();
            wendaStat.setUserId(userInfo.getId());
            wendaStat.setUsername(userInfo.getNickname());
            wendaStat.setLevel(userInfo.getLevel());
            wendaStat.setHeadUrl(userInfo.getHeadImgUrl());
            wendaStat.setAnswernum(wendaStatsCacheService.getScore(key, userId,type));
            list.add(wendaStat);
        }
        model.addAttribute(
                "rankList", list);
        return "wenda/wendaRankTpl";
    }
}
